package com.likeycy.my.set;

import com.likeycy.my.tree.BinaryTree;
import com.likeycy.my.tree.RedBlackTree;

import java.util.Comparator;

/**
 * @ClassName: MyTreeSet
 * @Description: 实现MyTreeSet
 * @Author: sodagreen
 * @Date: 2021/3/24 23:50
 * @Version: 1.0
 */
public class MyTreeSet<E> implements MySet<E>{

    private RedBlackTree<E> tree;

    public MyTreeSet() {
        this(null);
    }

    public MyTreeSet(Comparator<E> comparator) {
        tree = new RedBlackTree<>(comparator);
    }

    @Override
    public int size() {
        return tree.size();
    }

    @Override
    public boolean isEmpty() {
        return tree.isEmpty();
    }

    @Override
    public void clear() {
        tree.clear();
    }

    @Override
    public boolean contains(E element) {
        return tree.contains(element);
    }

    @Override
    public void add(E element) {
        // 红黑树自带去重
        tree.add(element);
    }

    @Override
    public void remove(E element) {
        tree.remove(element);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        tree.inorder(new BinaryTree.Visitor<E>() {
            @Override
            public boolean visit(E element) {
                return visitor.visit(element);
            }
        });
    }
}
